/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.edu.unifei.portal.dao;

import br.edu.unifei.portal.modelo.Tfg;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.Query;

/**
 *
 * @author Douglas Soares
 */
public class TfgDao {

    private EntityManager em;

    public TfgDao(EntityManager em) {
        this.em = em;
    }

    public void create(Tfg tfg) {
        em.getTransaction().begin();
        em.persist(tfg);
        em.getTransaction().commit();
    }

    public void update(Tfg tfg) {
        em.getTransaction().begin();
        em.merge(tfg);
        em.getTransaction().commit();
    }

    public void delete(int id) {
        em.getTransaction().begin();
        Tfg c = findTfg(id);
        em.remove(c);
        em.getTransaction().commit();
    }

    public Tfg findTfg(int id) {
        return (Tfg) em.createQuery(
                "from Tfg where id = '" + id + "'").getSingleResult();
    }

    public List<Tfg> findAllTfg() {
        return em.createQuery("from Tfg").getResultList();
    }

    public List<Tfg> findPageTfg(int max, int first) {
        Query query = em.createQuery("from Tfg");
        query.setMaxResults(max);
        query.setFirstResult(first);
        return query.getResultList();
    }

    public long getCountTfg() {
        Query query = em.createQuery("from Tfg");
        return (Long) em.createQuery(
                "select count(*) from Tfg").getSingleResult();
    }
}
